package com.zkca.xf.ui.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;



public abstract class SingleAdapter<T> extends RecyclerView.Adapter<ViewHolder> {

    protected Context mContext;
    protected List<T> dataList;
    protected int layoutId;
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_FOOTER = 1;
    public static final int TYPE_NORMAL = 2;
    //HeaderView, FooterView
    private View mHeaderView;
    private View mFooterView;

    public SingleAdapter(Context mContext, List<T> dataList, int layoutId) {
        this.mContext = mContext;
        this.dataList = dataList;
        this.layoutId = layoutId;
    }

//    public View getmHeaderView() {
//        return mHeaderView;
//    }

//    public void setmHeaderView(View mHeaderView) {
//        this.mHeaderView = mHeaderView; //shouweicharu
//        notifyItemInserted(0);
//    }
//
//    public View getmFooterView() {
//        return mFooterView;
//    }
//
//    public void setmFooterView(View mFooterView) {
//        this.mFooterView = mFooterView;
//        if (mHeaderView == null) {
//            notifyItemInserted(dataList.size());
//        } else {
//            notifyItemInserted(dataList.size() + 1);
//        }
//    }
//
//
//    @Override
//    public int getItemViewType(int position) {
//        if (mHeaderView == null) {
//            if (mFooterView == null) {
//                return TYPE_NORMAL;
//            } else {
//                if (position == dataList.size()) {
//                    return TYPE_FOOTER;
//                } else {
//                    return TYPE_NORMAL;
//                }
//            }
//        } else {
//            if (position == 0) {
//                return TYPE_HEADER;
//            } else {
//                if (mFooterView == null) {
//                    return TYPE_NORMAL;
//                } else {
//                    if (position != dataList.size() + 1) {
//                        return TYPE_NORMAL;
//                    } else {
//                        return TYPE_FOOTER;
//                    }
//                }
//            }
//        }
//
//
//    }
//
//
//    @Override
//    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
//        super.onAttachedToRecyclerView(recyclerView);
//        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//        if (layoutManager instanceof GridLayoutManager) {
//            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
//            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//                @Override
//                public int getSpanSize(int position) {
//                    return getItemViewType(position) == TYPE_HEADER ? gridLayoutManager.getSpanCount() : 1;
//                }
//            });
//        }
//    }
//
//
////    @Override
////    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
////        super.onViewAttachedToWindow(holder);
////        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
////        if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
////            StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) layoutParams;
////            params.setFullSpan(getItemViewType(holder.getLayoutPosition()) == TYPE_HEADER || getItemViewType(holder.getLayoutPosition()) == TYPE_FOOTER);
////        }
////    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
        return ViewHolder.createViewHolder(mContext, itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        convert(holder, dataList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public abstract void convert(ViewHolder holder, T t, int position);
}
